<?php

namespace youmbe\phpword;

use Yii;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\i18n\Formatter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
use PhpOffice\Common\Font;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
use PhpOffice\PhpWord\SimpleType\TextAlignment;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\TablePosition;
use youmbe\phpword\widgets\DetailView;
use PhpOffice\PhpWord\Shared\Html as PhpWordHtml;

/**
 * Word Widget for generate Word File or for load Word File.
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
Trait WordRenderTrait {

    /**
     * 按层级列表输出
     * @param \PhpOffice\PhpWord\Element\Section $section
     * @param array $data
     * @param integer $listStyle
     * @param integer $level
     */
    public static function dumpItem(&$section, $data, $listStyle, $level = 0) {

//        $objectWord->addNumberingStyle('hNum', array(
//            'type' => 'multilevel',
//            'levels' => array(
//                array('pStyle' => 'Heading4', 'format' => 'decimal', 'text' => '%1'),
//                array('pStyle' => 'Heading5', 'format' => 'decimal', 'text' => '%1.%2'),
//                array('pStyle' => 'Heading6', 'format' => 'decimal', 'text' => '%1.%2.%3'),
//            )
//        ));
//        $objectWord->addTitleStyle(4, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
//        $objectWord->addTitleStyle(5, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
//        $objectWord->addTitleStyle(6, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
//
//        $section->addTitle('标题1', 4);
//        $section->addTitle('标题2', 5);
//        $section->addTitle('标题3', 6);
//  $n = 1;
//    foreach ($lists as $list) {
//        // Hack to reset lists
//        $listFormat = $phpWord->addNumberingStyle(
//            'multilevel-'.$n,
//            array('type' => 'multilevel', 'levels' => array(
//                    array('format' => 'decimal', 'text' => '%1.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720)                    
//                )
//            )
//        );
//        $section->addText(htmlspecialchars('Multilevel list.'));
//        $section->addListItem(htmlspecialchars('List Item I'), 0, null, 'multilevel-'.$n);
//        $section->addListItem(htmlspecialchars('List Item I.a'), 1, null, 'multilevel-'.$n);
//        $section->addListItem(htmlspecialchars('List Item I.b'), 1, null, 'multilevel-'.$n);
//        $section->addListItem(htmlspecialchars('List Item II'), 0, null, 'multilevel-'.$n);
//        $section->addListItem(htmlspecialchars('List Item II.a'), 1, null, 'multilevel-'.$n);
//        $section->addListItem(htmlspecialchars('List Item III'), 0, null, 'multilevel-'.$n);
//        $section->addTextBreak(2);
//        $n++;
//    }
        foreach ($data as $item) {
            $title = ArrayHelper::getValue($item, 'title', '');
//$section->addListItem($title, $level, 'myOwnStyle', $listStyle, 'ListStyle');
            if ($level) {
                $styles = static::getMultiLevelStyle($level);
                $section->addListItem($title, $level, $styles['font'], $listStyle, $styles['paragraph']);
            } else {
                $section->addTitle($title, 3);
//                $title->getStyle();
            }

//            $item->setDocPart($docPart, $docPartId);
            $content = ArrayHelper::getValue($item, 'content', '');
            $contentFormat = ArrayHelper::getValue($item, 'format');
            if ($content) {
                if ($contentFormat == 'html') {
                    PhpWordHtml::addHtml($section, $content, false, false);
                } else {
//                    $section->addText($content);
                    $section->addPreserveText($content);
                }
            }
            $items = ArrayHelper::getValue($item, 'items', []);
            if (empty($items)) {
                continue;
            }
            static::dumpItem($section, $items, $listStyle, $level + 1);
        }
    }

    /**
     * 输出封面
     * @param \PhpOffice\PhpWord\Element\Section $section
     * @param array $data
     */
    public function renderCover(&$section, $data) {
        $title = ArrayHelper::getValue($data, 'title');
        $titleLevel = ArrayHelper::getValue($data, 'titleLevel');
        if ($title) {
            $section->addTitle($title, $titleLevel);
        }
        $columns = ArrayHelper::getValue($data, 'columns');
        DetailView::widget([
            'wordSection' => $section,
            'model' => $this->model,
            'view' => $this->activeWord,
            'attributes' => $columns,
            'rowHeight' => 36,
            'fontStyle' => [
                'name' => '华文中宋',
                'size' => 16,
                'bold' => true
            ],
            'paragraphStyle' => [
                'indentation' => [
                    'firstLine' => 0,
                ],
                'indent' => 0,
            ],
            'tableStyle' => [
                'position' => [
                    'tblpXSpec' => TablePosition::XALIGN_CENTER,
                    'tblpYSpec' => TablePosition::YALIGN_CENTER,
                ],
                'width' => 100
            ],
        ]);
    }

    /**
     * 输出目录
     * @param \PhpOffice\PhpWord\Element\Section $section
     * @param array $styles
     */
    public function renderToc(&$section, $styles = null) {
// Add text elements
        $section->addTitle('目录', 2);
// Add TOC #1

        $fontStyle = ['spaceAfter' => 60, 'size' => 12];
        $tocStyle = null;
        $minDepth = 2;
        $maxDepth = 4;
//        $toc = $section->addTOC($fontStyle12);

        $section->addTOC($fontStyle, $tocStyle, $minDepth, $maxDepth);
    }

    /**
     * 输出内容
     * @param \PhpOffice\PhpWord\Element\Section $section
     * @param array $data
     */
    public function renderPageContent(&$section, $data) {
        $title = ArrayHelper::getValue($data, 'title');
        $titleOptions = ArrayHelper::getValue($data, 'titleOptions');
        $titleLevel = ArrayHelper::getValue($titleOptions, 'level');
        if ($title) {
            $section->addTitle($title, $titleLevel);
        }
        $content = ArrayHelper::getValue($data, 'contents', []);
        $listStyle = 'multilevel' . $section->getSectionId();

        static::dumpItem($section, $content, $listStyle);
    }

    /**
     * 输出内容
     * @param \PhpOffice\PhpWord\Element\Section $section
     * @param array $data
     */
    public function renderPage(&$section, $data) {
        $title = ArrayHelper::getValue($data, 'title');
        $titleOptions = ArrayHelper::getValue($data, 'titleOptions');
        $titleLevel = ArrayHelper::getValue($titleOptions, 'level');
        if ($title) {
            $section->addTitle($title, $titleLevel);
        }
        $content = ArrayHelper::getValue($data, 'contents', []);
        $listStyle = 'multilevel' . $section->getSectionId();

        static::dumpItem($section, $content, $listStyle);
    }

    public function renderCustom(&$section, $data) {
        $section->addTitle('技术服务合同', 1);
// Adding Text element to the Section having font styled by default...
        $section->addText('"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)');
        $this->executeList($section);
    }

    public function renderDemo(&$section, $data = null) {

        $txt = '1111';
        $depth = 0;
        $font = null;
        $list = new \PhpOffice\PhpWord\Style\ListItem();
        $list->setListType(1);
        $para = null;

        $section->addListItem($txt, $depth, $font, $list, $para);

        /* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
        $section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
        $section->addText('"Learn from yesterday, live for today, hope for tomorrow. ' . 'The important thing is not to stop questioning." ' . '(Albert Einstein)');

        /*
         * Note: it's possible to customize font style of the Text element you add in three ways:
         * - inline;
         * - using named font style (new font style object will be implicitly created);
         * - using explicitly created font style object.
         */
// Adding Text element with font customized inline...
        $section->addText('"Great achievement is usually born of great sacrifice, ' . 'and is never the result of selfishness." ' . '(Napoleon Hill)', [
            'name' => 'Tahoma',
            'size' => 10
        ]);

// Adding Text element with font customized using named font style...
        $fontStyleName = 'oneUserDefinedStyle';
        $phpWord->addFontStyle($fontStyleName, [
            'name' => 'Tahoma',
            'size' => 10,
            'color' => '1B2232',
            'bold' => true
        ]);
        $section->addText('"The greatest accomplishment is not in never falling, ' . 'but in rising again after you fall." ' . '(Vince Lombardi)', $fontStyleName);

// Adding Text element with font customized using explicitly created font style object...
        $fontStyle = new \PhpOffice\PhpWord\Style\Font();
        $fontStyle->setBold(true);
        $fontStyle->setName('Tahoma');
        $fontStyle->setSize(13);
        $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
        $myTextElement->setFontStyle($fontStyle);
    }

    public function demolist($section) {
//添加列表元素
        $section->addListItem('ListItem 1', 0);
        $section->addListItem('ListItem 2', 0);
        $section->addListItem('ListItem 3', 0);
        $section->addTextBreak(2);

        $section->addListItem('ListItem 1', 0);
        $section->addListItem('ListItem 1.1', 1);
        $section->addListItem('ListItem 1.2', 1);
        $section->addListItem('ListItem 1.3(styled)', 1, ['bold' => true]);
        $section->addListItem('ListItem 1.3.1', 2);
        $section->addListItem('ListItem 1.3.2', 2);
        $section->addTextBreak(2);

        $listStyle = ['listType' => ListItemStyle::TYPE_NUMBER];
        $section->addListItem('ListItem 1', 0, null, $listStyle);
        $section->addListItem('ListItem 2', 0, null, $listStyle);
        $section->addListItem('ListItem 3', 0, null, $listStyle);
        $section->addTextBreak(2);


        $listStyle = ['listType' => ListItemStyle::TYPE_NUMBER_NESTED];
        $section->addListItem('ListItem 1', 0, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 2', 0, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 3', 1, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 4', 1, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 5', 2, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 6', 2, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 7', 1, 'myOwnStyle', $listStyle, 'P-Style');
        $section->addListItem('ListItem 8', 0, 'myOwnStyle', $listStyle, 'P-Style');
    }

}
